package com.dj.uphome.web.filter;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.dj.common.web.rest.ResponsePack;
import com.dj.common.web.rest.RestApiError;
import com.dj.core.DateUtil;
import com.dj.core.EncryptUtil;
import com.dj.core.MD5;

public class ServiceFilter implements Filter {
	protected Logger logger = LoggerFactory.getLogger(ServiceFilter.class);
	protected FilterConfig filterConfig = null;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	} 
	@Override
	public void destroy() {
		filterConfig = null;
	}
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		System.out.println(request.getRequestURL());
		if(request.getRequestURL().indexOf("swagger")>=0
				||request.getRequestURL().indexOf("webjars")>=0
				||request.getRequestURL().indexOf("api-docs")>=0) {
			chain.doFilter(request, response);
			return;
		}
		request.setCharacterEncoding("UTF-8");
		String json = request.getParameter("req_head");
		String req_data = request.getParameter("req_data");
		ParamRequest request0 = new ParamRequest(request);
		response.setContentType("application/json; charset=utf-8");  
		Map<String,Object> map = request0.getMap();
		String appKey = null;
		String timestamp =  null;
		String sign =  null;
		JSONObject json0 = null;
		if (StringUtils.isEmpty(json)) {
			if(null!=map){
				json0 = (JSONObject)map.get("req_head");
			}
		}else if (!StringUtils.isEmpty(json)) {
			json0 = JSONObject.parseObject(json);
		}
		if (StringUtils.isEmpty(req_data)) {
			if(null!=map){
				req_data = JSONObject.toJSONString(map.get("req_data"));
			}
		}
		if(null!=json0){
			appKey = json0.getString("app_key");
			timestamp =  json0.getString("timestamp");
			sign =  json0.getString("sign");
		}
		if(StringUtils.isEmpty(appKey)||StringUtils.isEmpty(timestamp)||StringUtils.isEmpty(sign)){//验证参数是否完整
			RestApiError rae = new RestApiError();
			rae.setCode("101");
			rae.setMessage("请求参数不全");
			ResponsePack<JSONObject> pp = new ResponsePack<>(rae);
			ServletOutputStream out = response.getOutputStream();
			out.write(JSONObject.toJSONString(pp).getBytes("UTF-8"));
			out.flush();
			out.close();
			return;
		}
		//Date currentDate = new Date();
		//Date maxDate = new Date(Long.parseLong(timestamp));
		/*if(currentDate.compareTo(DateUtil.addMinutes(maxDate, 2))>0){//验证请求是否过期
			RestApiError rae = new RestApiError();
			rae.setCode("102");
			rae.setMessage("请求已过期");
			ResponsePack<JSONObject> pp = new ResponsePack<>(rae);
			ServletOutputStream out = response.getOutputStream();
			out.write(JSONObject.toJSONString(pp).getBytes("UTF-8"));
			out.flush();
			out.close();
			return;
		}*/

		String appSecret = EncryptUtil.encrypt(appKey).toLowerCase();
		logger.info("sign:"+sign);
		logger.info("加密前:"+timestamp+req_data + appSecret);
		String sign2 = MD5.md5(timestamp+req_data + appSecret);		
		logger.info("加密后:"+sign2);
		/*if(!StringUtils.equals(sign2, sign)){//验证参数是否合法
			RestApiError rae = new RestApiError();
			rae.setCode("103");
			rae.setMessage("请求参数非法");
			ResponsePack<JSONObject> pp = new ResponsePack<>(rae);
			ServletOutputStream out = response.getOutputStream();
			out.write(JSONObject.toJSONString(pp).getBytes("UTF-8"));
			out.flush();
			out.close();
			return;
		}*/
		request0.setAttribute("platform", json0.getString("platform"));
		request0.setAttribute("partner_id", json0.getString("partner_id"));
		request0.setAttribute("version", json0.getString("version"));
		logger.info("请求地址:"+request.getRequestURL());
		chain.doFilter(request0, response);
	}
}
